﻿@{
    ViewBag.Title = "Home Page";
}
<p>
    GetLocations(): <a href="javascript:void(0);" onclick="testArray('getLocations')">Call Service</a><br />
    GetOccupations(): <a href="javascript:void(0);" onclick="testArray('getOccupations')">Call Service</a><br />
    GetInterests(): <a href="javascript:void(0);" onclick="testArray('getInterests')">Call Service</a><br />    
    <br />
    LoginAccount(user, pass): <a href="javascript:void(0);" onclick="testLoginAccount()">Call Service</a><br />
    SendMessage(date, message): <a href="javascript:void(0);" onclick="testSendMessage()">Call Service</a><br />
    EditAccount(user, location, interest): <a href="javascript:void(0);" onclick="testEditAccount()">Call Service</a><br />
    RegisterAccount(user, location, interest): <a href="javascript:void(0);" onclick="testRegisterAccount()">Call Service</a><br />
    RequestDate(dateId): <a href="javascript:void(0);" onclick="testRequestDate('6A245916-B09C-4E7A-ADE1-A2C8DC438FA7')">Call Service</a><br />
    GetUserImage(dateId): <a href="javascript:void(0);" onclick="getUserImage('7785845A-A2A1-4331-AE08-595B499940CD')">Call Service</a><br />
    GetUserFavorites(userId): <a href="javascript:void(0);" onclick="testArrayFavorites('7785845A-A2A1-4331-AE08-595B499940CD')">Call Service</a><br />
    SetUserFavorites(userId): <a href="javascript:void(0);" onclick="testSetFavorite('6A245916-B09C-4E7A-ADE1-A2C8DC438FA7')">Call Service</a><br />
</p>

<div>
    <form action="http://localhost:9000/AppDateService.svc/userImages" 
        method="post" enctype="multipart/form-data">
        <input type="hidden" id="userId" name="userId" value="7785845A-A2A1-4331-AE08-595B499940CD" />
        <input type="hidden" id="token" name="token" value="" />
        <input type="hidden" id="nonce" name="nonce" value="" />
        <input type="hidden" id="timestamp" name="timestamp" value="" />
        <div>
            <input type="file" id="uploadFile" name="UploadFile" />
        </div>
        <div>
            <input type="submit" value="Submit" />
        </div>
    </form>
</div>

<div id="resultDiv" style="color: #000;">
</div>

<script type="text/javascript">    
    var gToken = '';
    var gNonce = '';

    function getUserImage(userId) {
        $('#resultDiv').html('<img src="http://localhost:9000/AppDateService.svc/GetUserImage/' + gToken + '/' + gNonce + '/' + userId + '" />');
    }

    function testSetFavorite(favouriteUserId){
        jQuery.getJSON("http://localhost:9000/AppDateService.svc/setFavorite/" + gToken + "/" + gNonce + "/" + favouriteUserId,
            function (data) {
                var items = '';
                $.each(data.Favorites, function (key, val) {
                    items += '<li id="' + key + '">' + val["UserID"] + ':  ' + val["FavoriteUserName"] + '</li>';
                });
                $('#resultDiv').html('<ul>' + items + '</ul>');
            }
        );
    }

    function testArrayFavorites(userId) {
        jQuery.getJSON("http://localhost:9000/AppDateService.svc/getFavorites/" + gToken + "/" + gNonce + "/" + userId,
            function (data) {
                var items = '';
                $.each(data.Favorites, function (key, val) {
                    items += '<li id="' + key + '">' + val["UserID"] + ':  ' + val["FavoriteUserName"] + '</li>';
                });
                $('#resultDiv').html('<ul>' + items + '</ul>');
            }
        );
    }

    function testArray(func) {
        jQuery.getJSON("http://localhost:9000/AppDateService.svc/" + func,
            function (data) {
                var items = '';
                $.each(data, function (key, val) {
                    items += '<li id="' + key + '">' + val["Code"] + ':  ' + val["Description"] + '</li>';
                });
                $('#resultDiv').html('<ul>' + items + '</ul>');
            }
        );
    }

    function testLoginAccount() {
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "http://localhost:9000/AppDateService.svc/loginAccount",
            data: JSON.stringify({ UserName: "timor", Password: "mikkel" }),
            success: function (response) {
                $('#resultDiv').html(
                "<p>"
                    + "UserName: [" + response.User.UserName + "] <br/>"
                    + "Token: [" + response.Token + "] <br/>"
                    + "Nonce: [" + response.Nonce + "] <br/>"
                    + "Status: [" + response.Status + "] <br/>"
                    + "ErrorMessage: [" + response.ErrorMessage + "] <br/>"
                + "</p>");

                gToken = response.Token;
                gNonce = response.Nonce;

                $('#token').val(gToken);
                $('#nonce').val(gNonce);
            },
            error: function (errmsg) {
                alert('error ocured:' + errmsg.responseText);
            },
            complete: function () {
                //alert('completed'); 
            }
        });
    }

    function testSendMessage() {
        var text = prompt('input message');
        //var date = "\/Date(" + (new Date()).getTime() + ")\/";
        var objectJSON = {
            DateID: "71DDC57E-D3DD-4352-83A5-7D6FD1EE2F1C",
            FromUser: "7785845A-A2A1-4331-AE08-595B499940CD",
            MessageText: text,
            Token: gToken,
            Nonce: gNonce,
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "http://localhost:9000/AppDateService.svc/sendMessage",
            data: JSON.stringify(objectJSON),
            success: function (response) {
                $('#resultDiv').html(
                "<p>"
                    + "MessageID: [" + response.MessageID + "] <br/>"
                    + "Token: [" + response.Token + "] <br/>"
                    + "Nonce: [" + response.Nonce + "] <br/>"
                    + "Status: [" + response.Status + "] <br/>"
                    + "ErrorMessage: [" + response.ErrorMessage + "] <br/>"
                + "</p>");

                gToken = response.Token;
                gNonce = response.Nonce;
            },
            error: function (errmsg) {
                alert('error ocured:' + errmsg.responseText);
            },
            complete: function () {
                //alert('completed'); 
            }
        });
    }

    function testEditAccount() {
        var date = "\/Date(" + (new Date()).getTime() + ")\/";
        var objectJSON = {
            User: {
                ID: "6A245916-B09C-4E7A-ADE1-A2C8DC438FA7",
                UserName: "smurf",
                Password: "xx12!asd",
                Sex: 1,
                LookingForSex: 0,
                BirthDate: date,
                AgeFrom: 18,
                AgeTo: 30,
                AcceptOnlyMatchingAges: true,
                Location: "OSLO",
                HasPremium: false,
                Active: true,
                Description: "Testing user",
                OccupationCode: "TRANS"
            },
            Locations: ["HEDM", "OSLO", "TELE"],
            Interests: ["REISE", "TUR"],
            Token: gToken,
            Nonce: gNonce,
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "http://localhost:9000/AppDateService.svc/editAccount",
            data: JSON.stringify(objectJSON),
            success: function (response) {
                $('#resultDiv').html(
                "<p>"
                    + "Token: [" + response.Token + "] <br/>"
                    + "Nonce: [" + response.Nonce + "] <br/>"
                    + "Status: [" + response.Status + "] <br/>"
                    + "ErrorMessage: [" + response.ErrorMessage + "] <br/>"
                + "</p>");

                gToken = response.Token;
                gNonce = response.Nonce;
            },
            error: function (errmsg) {
                alert('error ocured:' + errmsg.responseText);
            },
            complete: function () {
                //alert('completed'); 
            }
        });
    }

    function testRegisterAccount() {
        var date = "\/Date(" + (new Date()).getTime() + ")\/";
        var objectJSON = {
            UserName: "mup11",
            Password: "xx12!asd",
            Sex: 1,
            LookingForSex: 0,
            BirthDate: date,
            AgeFrom: 18,
            AgeTo: 30,
            AcceptOnlyMatchingAges: true,
            Location: "OSLO",
            HasPremium: false,
            Active: true,
            Description: "Testing user",
            OccupationCode: "TRANS",
            Locations: ["HEDM", "OSLO", "TELE"],
            Interests: ["REISE", "TUR"]
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "http://localhost:9000/AppDateService.svc/registerAccount",
            data: JSON.stringify(objectJSON),
            success: function (response) {
                $('#resultDiv').html(response);

                gToken = response.Token;
                gNonce = response.Nonce;
            },
            error: function (errmsg) {
                alert('error ocured:' + errmsg.responseText);
            },
            complete: function () {
                //alert('completed'); 
            }
        });
    }

    function testRequestDate(userId) {
        jQuery.getJSON("http://localhost:9000/AppDateService.svc/requestDate/" + gToken + "/" + gNonce + "/" + userId,
            function (data) {
                $('#resultDiv').html(data.RequestDateStatus);
            }
        );
    }
</script>
